#pragma once
#include<iostream>
#include<string>
#include<time.h>
#include<stdarg.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>


#define Info    0
#define Debug   1
#define Error   2
#define Warning 3
#define Fatal   4

#define Screen    1
#define Onefile   2
#define Classfile 3

#define SIZE 1024

#define Logfile "log.txt"


class Log
{
public:
    Log()
    {
        PrintMethod=Screen;
        path="./log/";
    }
    void Enable(int method)
    {
        PrintMethod=method;
    }
    //等级转化为字符串
    std::string LeveltoString(int level)
    {
        switch(level)
        {
            case Info:
                return "Info";
            case Debug:
                return "Debug";
            case Error:
                return "Error";
            case Warning:
                return "Warning";
            case Fatal:
                return "Fatal";
            default:
                return "None";

        }
    }
    //日志的信息
    // void logmessage(int level,const char*format,...)
    // {
    //     time_t t=time(nullptr);
    //     struct tm* ctime=localtime(&t);
    //     char leftbuffer[SIZE];
    //     snprintf(leftbuffer,sizeof(leftbuffer),"[%s][%d-%d-%d-%d-%d-%d]",LeveltoString(level).c_str(),ctime->tm_year,ctime->tm_mon,
    //         ctime->tm_mday,ctime->tm_hour,ctime->tm_min,ctime->tm_sec);
        
    //     va_list s;
    //     va_start(s,format);
    //     char rightbuffer[SIZE];
    //     vsnprintf(rightbuffer,sizeof(rightbuffer),format,s);
    //     va_end(s);

    //     //格式：默认部分+自定义部分
    //     char logtxt[2*SIZE];
    //     snprintf(logtxt,sizeof(logtxt),"%s %s\n",leftbuffer,rightbuffer);

    //     printf("%s",logtxt);

    // }
    void Printlog(int level,const std::string& logtxt)
    {
        switch(PrintMethod)
        {
            case Screen:
                std::cout<<logtxt<<std::endl;
                break;
            case Onefile:
                PrintOnefile(Logfile,logtxt);
                break;
            case Classfile:
                PrintClassfile(level,logtxt);
                break;
            default:
                // std::cout<<"错了??"<<std::endl;
                break;
        }
    }
    void PrintOnefile(const std::string&logname,const std::string& logtxt)
    {
        std::string _logname=path+logname;
        //打开
        int fd=open(_logname.c_str(),O_WRONLY|O_CREAT|O_APPEND,0666);
        if(fd<0)
        {
            perror("open fail");
            return;
        }
        //写入
        write(fd,logtxt.c_str(),logtxt.size());
        //关闭
        close(fd);

    }

    void PrintClassfile(int level,const std::string& logtxt)
    {
        std::string filename=Logfile;
        filename+=".";
        filename+=LeveltoString(level);  //log.txt.Debug/Info.....
        PrintOnefile(filename,logtxt);
    }

    ~Log()
    {}

    void operator()(int level,const char*format,...)
    {
        time_t t=time(nullptr);
        struct tm* ctime=localtime(&t);
        char leftbuffer[SIZE];
        snprintf(leftbuffer,sizeof(leftbuffer),"[%s][%d-%d-%d-%d-%d-%d]",LeveltoString(level).c_str(),ctime->tm_year+1900,ctime->tm_mon+1,
            ctime->tm_mday,ctime->tm_hour,ctime->tm_min,ctime->tm_sec);
        
        va_list s;
        va_start(s,format);
        char rightbuffer[SIZE];
        vsnprintf(rightbuffer,sizeof(rightbuffer),format,s);
        va_end(s);

        //格式：默认部分+自定义部分
        char logtxt[2*SIZE];
        snprintf(logtxt,sizeof(logtxt),"%s %s\n",leftbuffer,rightbuffer);

        printf("%s",logtxt);
        // Printlog(level,logtxt);
    }
private:
    int PrintMethod;
    std::string path;
};